@inject DialogService DialogService
@inject ProductService ProductService
@inject NotificationService NotificationService
@inject SupplierService SupplierService

<RadzenStack>
    <RadzenTemplateForm EditContext="editContext" TItem="SupplierPayLog" Data="@model" Submit=@OnSubmit InvalidSubmit=@OnInvalidSubmit>
        <RadzenFieldset Text="付款供应商">
            <RadzenStack Gap="1rem">
                <RadzenStack class="rz-p-0"
                Orientation="Orientation.Vertical"
                AlignItems="AlignItems.Normal"
                JustifyContent="JustifyContent.SpaceBetween">
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="付款日期" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenDatePicker DateFormat="yyyy-MM-dd" TValue="DateTime?" @bind-Value="@model!.CreatedAt" ShowDays/>
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="付款金额" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenNumeric @bind-Value="@model!.Amount" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="备注" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextArea @bind-Value="@model!.Remark" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>
                </RadzenStack>
                <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
                    <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存" ButtonType="ButtonType.Submit" />
                </RadzenStack>
            </RadzenStack>
        </RadzenFieldset>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    [Parameter] public Supplier? PSupplier { get; set; }
    [Parameter] public SupplierPayLog? PLog { get; set; }
    private EditContext? editContext;
    private SupplierPayLog? model;

    void OnSubmit(SupplierPayLog model)
    {
        try
        {
            SupplierService.AddSupplierPayLog(model);
            DialogService.Close(model);
        }
        catch (Exception e)
        {
            DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    void OnInvalidSubmit(FormInvalidSubmitEventArgs args)
    {
        Console.WriteLine("invalid submit.");
    }

    private void OnCancel()
    {
        DialogService.Close(null);
    }

    protected override async Task OnInitializedAsync()
    {
        await Task.Run(() =>
        {
            model = PLog!;
            model.SupplierId = PSupplier!.Id;
            editContext = new EditContext(model);
        });
    }
}


